/******************************************************************
 * Created by: GOh CHee Hong                                      
 * Date of Creation: 1/Aug/2008             
 
 *                                                                
 * Description: This class is in charge of network web-of-trust
 ******************************************************************/

using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Sockets;
using System.Net;
using Pbdms.Common.CryptoUtils;
using Pbdms.Common.SharedVariables;
using Pbdms.Common.DataObject;
namespace Pbdms.BusinessLogic.trust
{
    public class trustProcessor
    {
        private KeyVerifyRequest req;
        private FriendsControl peerControl;
        private SecretKeyControl keyControl;
        private Pbdms.BusinessLogic.Network.NetworkController ntwkController;
        private PublicKeysControl publicKeyControl;
        public trustProcessor(KeyVerifyRequest req, FriendsControl peerControl, SecretKeyControl keyControl, Pbdms.BusinessLogic.Network.NetworkController ntwkController, PublicKeysControl publicKeyControl)
        {
            //init
            this.req = req;
            this.peerControl = peerControl ;
            this.keyControl = keyControl;
            this.ntwkController = ntwkController;
            this.publicKeyControl = publicKeyControl;
        }
        public void processVerifySignature()
        {

            Truster truster = new Truster(publicKeyControl);
            Queue<string> que = new Queue<string>();
            Boolean check = truster.checkTrust(publicKeyControl.getPublicKey(req.keyToBeVerifyID), keyControl.readSecretKey(),BusinessLogic.DataController.BuddyController.create().getListOfBuddies(),ref que);
            
            ///found a trust path
            if (check == true)
            {
                string spath = que.Dequeue();
                while (que.Count != 0)
                {
                    spath = spath + " -> " + que.Dequeue();
                }
                KeyVerifyResponse response = new KeyVerifyResponse();
                response.trustpath = spath;
                response.ipAddSource = Common.Global.getIP().ToString(); ;
                response.keyToBeVerifyID = req.keyToBeVerifyID;
                response.link = req.link;
                response.requestId = req.requestId;

                ntwkController.sendKeyToBeVerifyResponse(req.ipAddSource, response);
            }
            else
            {
                string original = req.link;
                int originalHop = req.curHop;
                if (req.curHop <= Pbdms.BusinessLogic.Network.NetworkController.MAX_HOPS)
                {
                    System.Collections.ArrayList arlBuddies = peerControl.getAuthenticatedFriendList();
                    if (arlBuddies != null && arlBuddies.Count > 0)
                    {
    
                        foreach (Common.DataObject.Buddy bdd in arlBuddies)
                        {

                            //if (!bdd.IpAdd.Equals(req.ipAddSource.ToString()))
                            {
                                //if ((req.ipAddForwarder == null
                                //|| (req.ipAddForwarder != null && !bdd.IpAdd.Equals(req.ipAddForwarder.ToString()))
                                //  ) && !bdd.IpAdd.Equals(NetworkDealer.NetworkDealer.ipAddMyComputer.ToString()))
                                {

                                    try
                                    {

                                        req.curHop=originalHop+1;
                                        req.ipAddForwarder = NetworkDealer.NetworkDealer.ipAddMyComputer.ToString();
                                        req.link = original + "->" + bdd.Name;


                                        ntwkController.forward_verifykeyRequest(bdd, req);
                                    }
                                    catch 
                                    {

                                    }
                                }
                            }
                        }
                    }
                }
            }

        }
    }
}
